<?php

namespace App\Http\Controllers\Wechat;

use App\Models\Wechat\User;
use App\Models\Wechat\UserLocation;

use App\Models\Config;
use App\Models\Wechat\Reply;
use App\Models\Wechat\Keword;

class EventController  
{
	//消息对象
	var $message;
	//全局app对象
    var $app;
    //用户openid
    var $openid; 

	public function __construct($message,$app){
		$this->message = $message;
		$this->app = $app;
	}

    public function entrance(){
    	$event = $this->message['Event'];
    	$this->openid = $this->message['FromUserName'];
    	switch ($event) {
    		case 'subscribe':
    			return $this->subscribe();
    		case 'unsubscribe':
    			return $this->unSubscribe();
    		case 'LOCATION' :
    			return $this->location();
    		case 'CLICK' :
    			return $this->click();
    		case 'VIEW' :
    			return $this->view();
    	}
    }

    /**
     * 关注
     * @return [type] [description]
     */
    function subscribe(){
    	$wechatUser = $this->app->user->get($this->openid);
    	$user = User::where(['openid'=>$this->openid])->first();
    	if(!$user){
    		$user = new User();
    		$user->openid = $this->openid;
    	}
		$user->nickname      = $wechatUser['nickname'];
		$user->sex           = $wechatUser['sex'];
		$user->province      = $wechatUser['province'];
		$user->city          = $wechatUser['city'];
		$user->country       = $wechatUser['country'];
		$user->headimgurl    = $wechatUser['headimgurl'] ? explode('/132', $wechatUser['headimgurl'])[0] : ''
		$user->subscribe     = 1;
		$user->subscribeTime = date('Y-m-d H:i:s',$wechatUser['subscribe_time']);
		$user->remark        = $wechatUser['remark'];
		$user->unionid       = isset($wechatUser['unionid']) ? $wechatUser['unionid'] : '';
		$user->groupid       = $wechatUser['groupid'];
		$user->tagid_list    = implode(',',$wechatUser['tagid_list']);
    	$user->save();

        $reply = new Reply();
        $config = new Config();
        $config = $config->cacheFindByKey($reply->staticName['follow']);
        if(!$config){
            return '';
        }
        $config->vue = json_decode($config->vue);
        $replyController = new ReplyController($this->app,$config->vue->wechat_image_id,$config->vue->wechat_mpnews_id,$config->vue->wechat_text_id);
        return $replyController->entrance();
    }

    /**
     * 取消关注
     * @return [type] [description]
     */
    function unSubscribe(){
    	$user = User::where(['openid'=>$this->openid])->first();
    	if($user){
    		$user->subscribe = 0;
    		$user->subscribeTime = date('Y-m-d H:i:s');
    		$user->save();
    	}
    	return '';
    }

    /**
     * 用户地理位置
     * @return [type] [description]
     */
    function location(){
    	
    	$userLocation = UserLocation::where(['openid'=>$this->openid])->first();
    	if(!$userLocation){
    		$userLocation = new UserLocation();
    	}
    	$userLocation->openid = $this->openid;
    	$userLocation->lat = $this->message['Latitude'];
    	$userLocation->lon = $this->message['Longitude'];
    	$userLocation->precision = $this->message['Precision'];
    	$userLocation->save();
    	return '';
    }

    /**
     * 点击菜单拉取消息
     * @return [type] [description]
     */
    function click(){
    	$eventKey = $this->message['EventKey'];
        $keyword = Keword::where('keyword',$eventKey)->first();
        if(!$keyword){
            return '';
        }
        $textController = new TextController($this->message,$this->app);
        return $textController->getKeywordReply($keyword);
    }

    /**
     * 点击菜单跳转链接
     * @return [type] [description]
     */
    function view(){
    	
    }


}
